package com.tancire.net.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import com.tancire.common.core.domain.entity.SysUser;
import com.tancire.common.enums.ExpenseType;
import com.tancire.common.utils.DateUtils;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.tancire.net.mapper.PurseLogMapper;
import com.tancire.net.domain.PurseLog;
import com.tancire.net.service.IPurseLogService;

/**
 * 会员钱包流水Service业务层处理
 *
 * @author chenchao
 * @date 2023-11-20
 */
@Service
public class PurseLogServiceImpl implements IPurseLogService {
    @Resource
    private PurseLogMapper purseLogMapper;

    /**
     * 查询会员钱包流水
     *
     * @param id 会员钱包流水主键
     * @return 会员钱包流水
     */
    @Override
    public PurseLog selectPurseLogById(Long id) {
        return purseLogMapper.selectPurseLogById(id);
    }

    /**
     * 查询会员钱包流水列表
     *
     * @param purseLog 会员钱包流水
     * @return 会员钱包流水
     */
    @Override
    public List<PurseLog> selectPurseLogList(PurseLog purseLog) {
        return purseLogMapper.selectPurseLogList(purseLog);
    }

    /**
     * 新增会员钱包流水
     *
     * @param purseLog 会员钱包流水
     * @return 结果
     */
    @Override
    public int insertPurseLog(PurseLog purseLog) {
        purseLog.setCreateTime(DateUtils.getNowDate());
        return purseLogMapper.insertPurseLog(purseLog);
    }

    /**
     * 修改会员钱包流水
     *
     * @param purseLog 会员钱包流水
     * @return 结果
     */
    @Override
    public int updatePurseLog(PurseLog purseLog) {
        purseLog.setUpdateTime(DateUtils.getNowDate());
        return purseLogMapper.updatePurseLog(purseLog);
    }

    /**
     * 批量删除会员钱包流水
     *
     * @param ids 需要删除的会员钱包流水主键
     * @return 结果
     */
    @Override
    public int deletePurseLogByIds(Long[] ids) {
        return purseLogMapper.deletePurseLogByIds(ids);
    }

    /**
     * 删除会员钱包流水信息
     *
     * @param id 会员钱包流水主键
     * @return 结果
     */
    @Override
    public int deletePurseLogById(Long id) {
        return purseLogMapper.deletePurseLogById(id);
    }

    /**
     * 查询今日获取积分
     *
     * @param userId
     * @return
     */
    @Override
    public BigDecimal selectPurseTodayPoints(Long userId) {
        return purseLogMapper.selectPurseTodayPoints(userId);
    }

    /**
     * 保存日志
     */
    @Override
    public void saveLog(Long purseId, BigDecimal points, String remark, int expenseType) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        PurseLog purseLog = new PurseLog();
        purseLog.setPurseId(purseId);
        purseLog.setUserId(user.getUserId());
        purseLog.setUserName(user.getNickName());
        purseLog.setExpenseType(expenseType);
        purseLog.setUseAmount(points);
        // 处理备注为空的情况
        if (StringUtils.isBlank(remark)) {
            if (ExpenseType.IN.getCode() == expenseType) {
                remark = "积分增加";
            } else {
                remark = "积分减少";
            }
        }
        purseLog.setUseRemarks(remark);
        purseLog.setCreateTime(new Date());
        purseLog.setCreateBy(user.getUserId() + "");
        this.purseLogMapper.insertPurseLog(purseLog);
    }
}
